package com.sduept.nwld.dataserver.manager.etl;

import com.sduept.core.dao.SuperDAO;
import com.sduept.bigdata.fault.common.ETLDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class BxSetValueSFETLManager {

	@Autowired
	private SuperDAO dao;
	
	@Autowired
	private ETLDAO eDao;
	
	public  void receiveAndSaveAllData() {
		List<?> list = getAllSourceData();
		receiveAndSave(list);
	}
	public  void receiveAndSavePartData(String date) {
		List<?> list = getAllSourceData(date);
		receiveAndSave(list);
	}
	
	
	public List<?> getAllSourceData(){
		String sql="select mPrivateId,aliasname,finalvalue,maxval,minval,mStationId,mCurTime,mStationName from t_bx_setvalue ";
		List <?>result = eDao.query(sql);
		return result;
	}
	public List<?> getAllSourceData(String date) {
		String sql = "select mPrivateId,aliasname,finalvalue,maxval,minval,mStationId,mCurTime,mStationName from t_bx_setvalue "
				+ " where update_time=";
		List<?> result = eDao.query(sql + "'" + date + "'");
		return result;
	}
	public  void receiveAndSave(List<?> list) {
		ETLDataSave equipmentSave = new ETLDataSave(list);
		equipmentSave.saveToBean();
	}
	public class ETLDataSave{
		private List<?> list;
 
		public ETLDataSave(List<?> list) {
			super();
			this.list = list;
		}
		public void saveToBean(){
			String sql="insert into t_bx_setvalue(mPrivateId,aliasname,finalvalue,maxval,minval,mStationId,mCurTime,mStationName) values(?,?,?,?,?,?,?)";
			 List<Object[]> datas = new ArrayList<Object[]>();
			for(Object obj : list){
				Object[] objs = (Object[]) obj;
				for(int i=0;i<objs.length;i++){
					if(objs[i]!=null&&"null".equals(objs[i].toString())){
						objs[i]=null;
					}
				}
				if(objs[0]!=null){
					datas.add(objs);
				}
			}
			try {
				dao.persistentBatch(sql, datas);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
